package com.shop.elasticsearch.controller;

import com.shop.elasticsearch.bean.ProductEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @author Song Yinghui
 * @date 2019/12/14
 * @Version 1.0
 */
@RestController(value = "/ela")
public class TestController {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @RequestMapping(value = "/save")
    public String test(){

        ProductEntity entity = new ProductEntity();
        entity.setId(UUID.randomUUID().toString().replaceAll("-",""));
        entity.setProductName("测试商品名称");
        entity.setProductDesc("这是一个商品描述");
        entity.setNormId("12312");
        entity.setProductPrice(12.12);
        entity.setCreateTime(new Date());
        IndexQuery indexQuery = new IndexQuery();
        indexQuery.setObject(entity);
        String index = elasticsearchTemplate.index(indexQuery);
        return index;
    }
}
